package fr.kotton.myorganizer.utils.sql;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import fr.kotton.myorganizer.db.MyOrganizerDbHelper;
import fr.kotton.myorganizer.task.interfaces.TasksQueryCompleteListener;

public class DatabaseUpdater extends AsyncTask<SqlQuery, Void, Cursor> {

    private MyOrganizerDbHelper mDbHelper;

    public DatabaseUpdater(Context pContext, TasksQueryCompleteListener SqlQueryCompleteListener) {
        super();
        mDbHelper = new MyOrganizerDbHelper(pContext);
        // mTasksCallback = SqlQueryCompleteListener;
    }

    @Override
    protected Cursor doInBackground(SqlQuery... pSqlUpdateQueries) {

        SQLiteDatabase lDatabase = mDbHelper.getReadableDatabase();

        Cursor lCursor = null;

        if (pSqlUpdateQueries[0] instanceof SqlUpdateQuery) {

            SqlUpdateQuery lSqlUpdateQuery = (SqlUpdateQuery) pSqlUpdateQueries[0];

            lDatabase.update(
                    lSqlUpdateQuery.getTable(),                    // The table to query
                    lSqlUpdateQuery.getValues(),                               // The columns to update
                    lSqlUpdateQuery.getWhereClause(),                            // The WHERE clause
                    lSqlUpdateQuery.getWhereArgs()                                     // The values for the WHERE clause
            );

        }

        return null;
    }

    @Override
    protected void onPostExecute(Cursor pCursor) {
        // TODO Ajouter un retour visuel utilisateur
    }
}
